/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.GUI.popup;

import controller.Constants;
import controller.Serializer;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import model.codebehind.CodeModel;
import model.graphic.GraphicModel;
import model.graphic.objects.GBoxNode;
import model.graphic.objects.GConnection;
import model.graphic.objects.GNode;
import model.graphic.objects.GOutput;
import model.graphic.objects.GTrigger;
import view.GUI.actions.AddAction;
import view.GUI.actions.ColorAction;
import view.GUI.actions.DeBoxAction;
import view.GUI.actions.DeleteAction;
import view.GUI.actions.GridAction;
import view.GUI.actions.ReDrawAction;
import view.GUI.actions.SetTriggerAction;
import view.GUI.actions.SnapAction;
import view.GUI.graphic.GraphicView;
import view.GUI.ConnectFileFilter;
import view.GUI.LabelReader;
import view.GUI.actions.AddExpandedNodeAction;
import view.GUI.actions.EditStoreAction;
import view.GUI.actions.HideAllPortsAction;
import view.GUI.actions.RemoveConnectionAction;
import view.GUI.actions.ReplaceConnectionAction;

/**
 *
 * @author Jelle, modified by Ben
 */
public class ViewPopup implements IPopup {

    private JPopupMenu popup = new JPopupMenu();
    private JMenuItem m;
    private GraphicView gview;
    private GraphicModel gmodel;
    private Constants cons = new Constants();
    private JFrame frame;
    private CodeModel cmodel;

    public ViewPopup() {
    }

    public ViewPopup(GraphicView gview, CodeModel cmodel, GraphicModel gmodel, JFrame frame) {
        this.gview = gview;
        this.frame = frame;
        this.cmodel = cmodel;
        this.gmodel = gmodel;
    }

    public void clearMenu() {
        popup.removeAll();
    }

    public void menuPanel() {
        clearMenu();
        m = new JMenuItem(new DeleteAction(true, gview));
        m.setText(LabelReader.getInstance().getString("ClearAll"));
        popup.add(m);
        popup.addSeparator();
        m = new JCheckBoxMenuItem(new GridAction(gview));
        m.setSelected(gview.isGridOn());
        m.setText(LabelReader.getInstance().getString("Grid"));
        popup.add(m);
        m = new JCheckBoxMenuItem(new SnapAction(gview));
        m.setSelected(gview.isSnapped());
        m.setText(LabelReader.getInstance().getString("SnapToGrid"));
        popup.add(m);
    }

    public void menuOutput(GOutput output, Point point){
        clearMenu();
        m = new JMenuItem(new AddExpandedNodeAction(gview,gmodel,output,point));
        m.setText(LabelReader.getInstance().getString("Expand"));
        popup.add(m);
    }

    public void menuConnection(GConnection conn) {
        clearMenu();
        m = new JMenuItem(new RemoveConnectionAction(conn,gview));
        m.setText(LabelReader.getInstance().getString("DeleteConnection"));
        popup.add(m);
        m = new JMenuItem(new ReplaceConnectionAction(conn,gview));
        m.setText(LabelReader.getInstance().getString("ReplaceConnection"));
        popup.add(m);
    }

    public void menuTrigger(GTrigger trigger) {
        clearMenu();
        m = new JCheckBoxMenuItem(new SetTriggerAction(gview, gmodel, gmodel.getSelected()));
        m.setText(LabelReader.getInstance().getString("SetThisAsMainTrigger"));
        if (trigger == gmodel.getMainTrigger()) {
            m.setSelected(true);
        } else {
            m.setSelected(false);
        }
        popup.add(m);
    }

    public void menuNode() {
        clearMenu();
        m = new JMenuItem(new DeleteAction(false, gview, gmodel.getSelected()));
        m.setText(LabelReader.getInstance().getString("DeleteNode"));
        popup.add(m);
        popup.addSeparator();
        m = new JMenuItem(new ReDrawAction(gview, gmodel.getSelected()));
        m.setText(LabelReader.getInstance().getString("Redraw"));
        popup.add(m);
        m = new JMenuItem(new ColorAction(gview, gmodel.getSelected()));
        m.setText(LabelReader.getInstance().getString("ChangeColor"));
        popup.add(m);
        m = new JMenuItem(new HideAllPortsAction(gview,gmodel, gmodel.getSelected()));
        m.setText(LabelReader.getInstance().getString("HideAllPorts"));
        popup.add(m);
    }

    public void menuStore() {
        clearMenu();
        m = new JMenuItem(new DeleteAction(false, gview));
        m.setText(LabelReader.getInstance().getString("DeleteNode"));
        popup.add(m);
        m = new JMenuItem(new EditStoreAction(gview, frame, (GNode) gmodel.getSelected()));
        m.setText(LabelReader.getInstance().getString("ChangeType"));
        popup.add(m);
        popup.addSeparator();
        m = new JMenuItem(new ReDrawAction(gview, gmodel.getSelected()));
        m.setText(LabelReader.getInstance().getString("Redraw"));
        popup.add(m);
        m = new JMenuItem(new ColorAction(gview, gmodel.getSelected()));
        m.setText(LabelReader.getInstance().getString("ChangeColor"));
        popup.add(m);
    }

    public void menuBoxedInNode() {
        clearMenu();
        m = new JMenuItem(new DeBoxAction(gview));
        m.setText(LabelReader.getInstance().getString("DeboxNode"));
        popup.add(m);
        m = new JMenuItem(new DeleteAction(false, gview, gmodel.getSelected()));
        m.setText(LabelReader.getInstance().getString("DeleteNode"));
        popup.add(m);
        popup.addSeparator();
        m = new JMenuItem(new ReDrawAction(gview, gmodel.getSelected()));
        m.setText(LabelReader.getInstance().getString("Redraw"));
        popup.add(m);
        m = new JMenuItem(new ColorAction(gview, gmodel.getSelected()));
        m.setText(LabelReader.getInstance().getString("ChangeColor"));
        popup.add(m);
    }

    public void menuBoxNode() {
        clearMenu();
        m = new JMenuItem(new DeleteAction(false, gview, gmodel.getSelected()));
        m.setText(LabelReader.getInstance().getString("DeleteBoxNode"));
        popup.add(m);
        popup.addSeparator();
        m = new JMenuItem(LabelReader.getInstance().getString("ExportBoxNode"));
        m.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JFileChooser filechooser = new JFileChooser(".");
                filechooser.setFileFilter(new ConnectFileFilter(".bn (BoxNode)", new String[]{"bn", "boxnode"}));
                int returnVal = filechooser.showSaveDialog(gview);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = filechooser.getSelectedFile();
                    if (GBoxNode.class.isAssignableFrom(gmodel.getSelected().getClass())){
                        GBoxNode gbn = (GBoxNode) gmodel.getSelected();
                        String path = file.getPath();
                        if (!file.getPath().endsWith(".bn")) {
                            path = path + ".bn";
                        }
                        Serializer s = new Serializer(gmodel, cmodel, gbn, path);
                        s.serializeBoxNode();
                    }

                }
            }
        });
        popup.add(m);
        popup.addSeparator();
        JMenu m2 = new JMenu(LabelReader.getInstance().getString("AddHandle"));
        m = new JMenuItem(new AddAction(cons.getValueTransmitterInString(), gview, frame));
        m.setText(LabelReader.getInstance().getString("NewValueTransmitterIn"));
        m2.add(m);
        m = new JMenuItem(new AddAction(cons.getValueTransmitterOutString(), gview, frame));
        m.setText(LabelReader.getInstance().getString("NewValueTransmitterOut"));
        m2.add(m);
        m = new JMenuItem(new AddAction(cons.getTriggerTransmitterInString(), gview, null));
        m.setText(LabelReader.getInstance().getString("NewTriggerTransmitterIn"));
        m2.add(m);
        m = new JMenuItem(new AddAction(cons.getTriggerTransmitterOutString(), gview, null));
        m.setText(LabelReader.getInstance().getString("NewTriggerTransmitterOut"));
        m2.add(m);

        popup.add(m2);
        popup.addSeparator();
        m = new JMenuItem(new ReDrawAction(gview, gmodel.getSelected()));
        m.setText(LabelReader.getInstance().getString("Redraw"));
        popup.add(m);
        m = new JMenuItem(new ColorAction(gview, gmodel.getSelected()));
        m.setText(LabelReader.getInstance().getString("ChangeColor"));
        popup.add(m);
    }

    public void menuBoxedInBoxNode() {
        clearMenu();
        m = new JMenuItem(new DeBoxAction(gview));
        m.setText(LabelReader.getInstance().getString("DeboxBoxNode"));
        popup.add(m);
        m = new JMenuItem(new DeleteAction(false, gview));
        m.setText(LabelReader.getInstance().getString("DeleteBoxNode"));
        popup.add(m);
        popup.addSeparator();
        JMenu m2 = new JMenu(LabelReader.getInstance().getString("AddHandle"));
        m = new JMenuItem(new AddAction(cons.getValueTransmitterInString(), gview, frame));
        m.setText(LabelReader.getInstance().getString("NewValueTransmitterIn"));
        m2.add(m);
        m = new JMenuItem(new AddAction(cons.getValueTransmitterOutString(), gview, frame));
        m.setText(LabelReader.getInstance().getString("NewValueTransmitterOut"));
        m2.add(m);
        m = new JMenuItem(new AddAction(cons.getTriggerTransmitterInString(), gview, null));
        m.setText(LabelReader.getInstance().getString("NewTriggerTransmitterIn"));
        m2.add(m);
        m = new JMenuItem(new AddAction(cons.getTriggerTransmitterOutString(), gview, null));
        m.setText(LabelReader.getInstance().getString("NewTriggerTransmitterOut"));
        m2.add(m);

        popup.add(m2);
        popup.addSeparator();
        m = new JMenuItem(new ReDrawAction(gview, gmodel.getSelected()));
        m.setText(LabelReader.getInstance().getString("Redraw"));
        popup.add(m);
        m = new JMenuItem(new ColorAction(gview, gmodel.getSelected()));
        m.setText(LabelReader.getInstance().getString("ChangeColor"));
        popup.add(m);
    }

    public JPopupMenu getPopup() {
        return popup;
    }
}
